# -*- coding: utf-8 -*-
"""
Created on Sun Aug  2 18:10:04 2020

@author: soapy
"""

import os

tempfile = 'temp.txt'

def map_friends():
	with open('./friends.txt','r') as fp:
		strFriendlist = fp.readlines()

	with open(tempfile, 'w') as fp2:
		#friendPair_d = {}
		strOut = []
		for row in strFriendlist:
			if row.strip() != '':
				host = row.split(':')[0]
				friends_l = row.split(':')[1].strip().split(' ')
				# print(host, friends_l)
				for a in range(0, len(friends_l)-1):
					for b in range(a+1, len(friends_l)):
						#friendPair_d[(friends_l[a], friends_l[b])] = host
						strOut.append('%s %s %s'%(friends_l[a], friends_l[b], host))
						fp2.write('%s %s %s\n'%(friends_l[a], friends_l[b], host))

	return strOut
#
def reduce_friends():
	with open(tempfile, 'r') as fp2:
		friends_lst = fp2.readlines()

	friend_dict = {}
	for line in friends_lst:
		# print(line)
		host = line.strip().split(' ')[2]

		if host not in friend_dict.keys():
			frends_s = set()
			frends_s.add(line.strip().split(' ')[0])
			frends_s.add(line.strip().split(' ')[1])
			friend_dict[host] = frends_s
		else:
			frends_s = friend_dict[host]
			frends_s.add(line.strip().split(' ')[0])
			frends_s.add(line.strip().split(' ')[1])

	return friend_dict

if __name__ == '__main__':
	map_friends()
	friend_dict = reduce_friends()
	for key in friend_dict.keys():
		print(key, friend_dict[key])
